//
//  ViewController.m
//  BannerViewDemo
//
//  Created by BaizYi on 2018/9/26.
//  Copyright © 2018年 BaizYi. All rights reserved.
//

#import "ViewController.h"
#import "YJYBannerView.h"
#import "UIView+Frame.h"
#import "RMImageBrowser.h"
#import "CollectionViewCell.h"
#import "YJYBrowsersInterActiveAnimationTransition.h"
#import "YJYBrowsersViewController.h"
#import "YJYCarouselView.h"

static NSString *const ID = @"cell";

@interface ViewController ()<YJYBannerViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property(strong,nonatomic) UICollectionView *collectionView;
@property(strong,nonatomic) YJYBannerView  *bannerView;
@property(strong,nonatomic) NSArray  *dataList;
@property(strong,nonatomic) NSMutableArray  *imageList;

@property(strong,nonatomic) YJYBrowsersInterActiveAnimationTransition  *animatedTransition;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *imageArr = @[@"http://221.228.226.5/15/t/s/h/v/tshvhsxwkbjlipfohhamjkraxuknsc/sh.yinyuetai.com/88DC015DB03C829C2126EEBBB5A887CB.mp4", @"pic1.jpeg", @"pic2.jpeg", @"pic3.jpeg", @"pic4.jpeg", @"pic5.jpeg"];
    
    
    //无限轮播图(只有图片并自动轮播)
//    YJYBannerView *bannerView = [[YJYBannerView alloc]initWithImageArr:imageArr.mutableCopy frame:CGRectMake(0, 20, self.view.width, 200)];
//    self.bannerView = bannerView;
//    bannerView.delegate = self;
//    bannerView.backgroundColor = [UIColor lightGrayColor];
//    [self.view addSubview:bannerView];
    
    //手动轮播(可以是视频可以图片)
    YJYCarouselView *carouselView = [[YJYCarouselView alloc] scrollViewFrame:CGRectMake(0, 20, self.view.frame.size.width, 200) imageArray:imageArr];
    [self.view addSubview:carouselView];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.center = self.view.center;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = [UIImage imageNamed:@"pic1.jpeg"];
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
    [imageView addGestureRecognizer:tap];
    [self.view addSubview:imageView];
    
    [self.view addSubview:self.collectionView];
    
    
}

-(void)onTap:(UIGestureRecognizer *)tap{
    [RMImageBrowser showImage:(UIImageView *)tap.view];
}

-(void)bannerView:(YJYBannerView *)bannerView selectAtIndex:(NSInteger)index{
    NSLog(@"selectAtIndex:%li", index);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.image = [UIImage imageNamed:self.dataList[indexPath.row]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSInteger index = indexPath.row;
    
    
    
    YJYPictureBrowsersTransitionParamer *transitionParamer = [[YJYPictureBrowsersTransitionParamer alloc]init];
    transitionParamer.transitionImage = cell.photoView.image;
    transitionParamer.firstVCImageFrames = [self firstImageViewFrames];
    transitionParamer.transitionImgIndex = index;
    
    self.animatedTransition = nil;
    self.animatedTransition.transitionParamer = transitionParamer;
    
    YJYBrowsersViewController *vc = [[YJYBrowsersViewController alloc]init];
    vc.dataSource = self.dataList;
    vc.animatedTransition = self.animatedTransition;
    
    vc.transitioningDelegate = self.animatedTransition;
    [self presentViewController:vc animated:YES completion:nil];
}


-(NSArray<NSValue *> *)firstImageViewFrames{
    NSMutableArray *imageFrames = [NSMutableArray array];
    for (NSInteger i=0; i<self.dataList.count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        CollectionViewCell *cell = (CollectionViewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
        if (cell.photoView) {
            CGRect frame = [self getFrameInWindow:cell.photoView];
            [imageFrames addObject:[NSValue valueWithCGRect:frame]];
        }else{
            CGRect frame = CGRectZero;
            [imageFrames addObject:[NSValue valueWithCGRect:frame]];
        }
    }
    return imageFrames;
}
-(CGRect)getFrameInWindow:(UIView *)view{
    return [view.superview convertRect:view.frame toView:nil];
}

-(void)imageViewClick:(NSInteger)cellIndex{
    [self dismissViewControllerAnimated:YES completion:nil];
}



-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        
        CGFloat margin = 5;
        CGFloat itemWH = (self.view.width - 3 * margin) / 3;
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(itemWH, itemWH);
        layout.minimumLineSpacing = margin;
        layout.minimumInteritemSpacing = margin;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.bannerView.frame) + 10, self.view.width, self.view.height - self.bannerView.height) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor blackColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:ID];
        
        
    }
    return _collectionView;
}
-(NSArray *)dataList{
    if (_dataList == nil) {
        _dataList = @[@"pic1.jpeg", @"pic2.jpeg", @"pic3.jpeg", @"pic4.jpeg", @"pic5.jpeg"];
    }
    return _dataList;
}
-(NSMutableArray *)imageList{
    if (_imageList == nil) {
        _imageList = [NSMutableArray array];
    }
    return _imageList;
}

-(YJYBrowsersInterActiveAnimationTransition *)animatedTransition{
    if (_animatedTransition == nil) {
        _animatedTransition = [[YJYBrowsersInterActiveAnimationTransition alloc]init];
    }
    return _animatedTransition;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
